package com.whz.workmanageol.service.impl;

import com.whz.workmanageol.dao.*;
import com.whz.workmanageol.entity.*;
import com.whz.workmanageol.service.ExcelReadService;
import com.whz.workmanageol.utils.FileDownload;
import com.whz.workmanageol.utils.ReadExcel;
import com.whz.workmanageol.utils.UUIDGenerator;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Service
@Transactional
public class ExcelReadServiceImpl implements ExcelReadService {

    private static final Logger logger = LoggerFactory.getLogger(ExcelReadServiceImpl.class);

    @Autowired
    private ReadExcel readExcel;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private GradeCourseMapper gradeCourseMapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Autowired
    private TeacherCourseMapper teacherCourseMapper;
    @Autowired
    private TeacherGradeMapper teacherGradeMapper;
    @Override
    public void studentInfo(MultipartFile file) {
        File studentInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "studentinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), studentInfoFile);
            List<Student> studentList = readExcel.readStudent(studentInfoFile);
            for(Student student : studentList){
                student.setId(UUIDGenerator.getUUID());
                studentMapper.insert(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void teacherInfo(MultipartFile file) {
        File teacherInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "teacherinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), teacherInfoFile);
            List<Teacher> teacherList = readExcel.readTeacher(teacherInfoFile);
            for(Teacher teacher : teacherList){
                teacher.setId(UUIDGenerator.getUUID());
                teacherMapper.insert(teacher);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void gradeInfo(MultipartFile file) {
        File gradeInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "gradeinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), gradeInfoFile);
            List<Grade> gradeList = readExcel.readGrade(gradeInfoFile);
            for(Grade grade : gradeList){
                grade.setId(UUIDGenerator.getUUID());
                gradeMapper.insert(grade);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void courseInfo(MultipartFile file) {
        File courseInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "courseinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), courseInfoFile);
            List<Course> courseList = readExcel.readCourse(courseInfoFile);
            for(Course course : courseList){
                course.setId(UUIDGenerator.getUUID());
                courseMapper.insert(course);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void gradeCourseInfo(MultipartFile file) {
        File gradeCourseInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "gradecourseinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), gradeCourseInfoFile);
            List<GradeCourse> gradeCourseList = readExcel.readGradeCourse(gradeCourseInfoFile);
            for(GradeCourse gradeCourse : gradeCourseList){
                gradeCourse.setId(UUIDGenerator.getUUID());
                gradeCourseMapper.insert(gradeCourse);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void studentGradeInfo(MultipartFile file) {
        File stuGradeInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "studentgradeinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), stuGradeInfoFile);
            readExcel.readStuGrade(stuGradeInfoFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void studentCourseInfo(MultipartFile file) {
        File stuCourseInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "studentcourseinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), stuCourseInfoFile);
            List<StudentCourse> studentCourseList = readExcel.readStuCourse(stuCourseInfoFile);
            for(StudentCourse studentCourse : studentCourseList){
                studentCourse.setId(UUIDGenerator.getUUID());
                studentCourseMapper.insert(studentCourse);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void teaGradeInfo(MultipartFile file) {
        File teaGradeInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "teachergradeinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), teaGradeInfoFile);
            List<TeacherGrade> teacherGradeList = readExcel.readTeaGrade(teaGradeInfoFile);
            for(TeacherGrade teacherGrade : teacherGradeList){
                teacherGrade.setId(UUIDGenerator.getUUID());
                teacherGradeMapper.insert(teacherGrade);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void teaCourseInfo(MultipartFile file) {
        File teaCourseInfoFile = new File(FileDownload.EXCELEXAMLE + File.separator + new Date().getTime() + "teachercourseinfo.xlsx");
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), teaCourseInfoFile);
            List<TeacherCourse> teacherCourseList = readExcel.readTeaCourse(teaCourseInfoFile);
            for(TeacherCourse teacherCourse : teacherCourseList){
                teacherCourse.setId(UUIDGenerator.getUUID());
                teacherCourseMapper.insert(teacherCourse);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
